package com.xudow.app.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.activeshare.edu.ucenter.models.base.AdvInfo;
import com.xudow.app.Config;
import com.xudow.app.R;
import com.xudow.app.ui.task.AdvertisementTask;
import com.xudow.app.ui.widget.gallery.XViewPaper;
import com.xudow.app.util.AdvertisementUtils;
import com.xudow.app.util.ImageUtils;
import com.xudow.app.util.Maps;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * 我的考勤Activity
 *
 * @author lukecheng
 */
public class ClazzLogActivity extends BaseActivity {

    private static final String TYPE_CURRENT_WEEK = "1"; //本周
    private static final String TYPE_CURRENT_MONTH = "2"; //本月
    private static final String TYPE_ALL = "3"; //全部

    private static final String[] TYPES = {TYPE_CURRENT_WEEK, TYPE_CURRENT_MONTH, TYPE_ALL};

    private static final int NUM_ITEMS = 3;

    private SectionsPagerAdapter pagerAdapter;
    private ViewPager viewPager;

    private TextView curWeekTab, curMonthTab, allTab;

    private String studentId;

    /**
     * 广告图片
     */
    private XViewPaper xViewPaper;
    private LinearLayout ovalLayout;
    private List<View> listViews;
    private List<AdvInfo> advs;
    private RelativeLayout advLayout;

    private AdvertisementTask advertisementTask;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clazz_log);

        studentId = getIntent().getStringExtra("studentId");

        xViewPaper = (XViewPaper) findViewById(R.id.gallery);
        ovalLayout = (LinearLayout) findViewById(R.id.vb);
        advLayout = (RelativeLayout) findViewById(R.id.ad);

        curWeekTab = (TextView) findViewById(R.id.cur_week_tab);
        curMonthTab = (TextView) findViewById(R.id.cur_month_tab);
        allTab = (TextView) findViewById(R.id.all_tab);

        pagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), studentId);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        viewPager.setAdapter(pagerAdapter);
        viewPager.setCurrentItem(0);

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                viewPager.setCurrentItem(position);
                changeTabSelectionState(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

        //加载广告
        HashMap<String, String> advParam = Maps.newHashMap();
        advParam.put("app_page", "3"); //考勤列表页

        advertisementTask = new AdvertisementTask(this, advertisementTaskHandler);
        addTask(advertisementTask);
        advertisementTask.execute(advParam);
    }

    private void changeTabSelectionState(int position) {
        curWeekTab.setBackgroundResource(0);
        curWeekTab.setTextColor(getResources().getColor(R.color.text_medium));
        allTab.setBackgroundResource(0);
        allTab.setTextColor(getResources().getColor(R.color.text_medium));
        curMonthTab.setBackgroundResource(0);
        curMonthTab.setTextColor(getResources().getColor(R.color.text_medium));

        switch (position) {
            case 0:
                curWeekTab.setBackgroundResource(R.mipmap.tab_selected_bottom_border);
                curWeekTab.setTextColor(getResources().getColor(R.color.rvcode));
                break;
            case 1:
                curMonthTab.setBackgroundResource(R.mipmap.tab_selected_bottom_border);
                curMonthTab.setTextColor(getResources().getColor(R.color.rvcode));
                break;
            case 2:
                allTab.setBackgroundResource(R.mipmap.tab_selected_bottom_border);
                allTab.setTextColor(getResources().getColor(R.color.rvcode));
                break;
            default:
                break;
        }
    }

    public void onBackBtnClick(View view) {
        finish();
    }

    public void onTabClick(View view) {
        int index = 0;

        switch (view.getId()) {
            case R.id.cur_week_tab:
                index = 0;
                break;
            case R.id.cur_month_tab:
                index = 1;
                break;
            case R.id.all_tab:
                index = 2;
                break;
        }

        changeTabSelectionState(index);
        viewPager.setCurrentItem(index);
    }

    /**
     * 广告加载Handler
     */
    private Handler advertisementTaskHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (AdvertisementTask.SUCCESS == msg.what) {
                AdvInfo[] dataArr = (AdvInfo[]) msg.getData().getSerializable("advs");

                if (null == dataArr || dataArr.length <= 0) {
                    return;
                }

                advs = new ArrayList<AdvInfo>();
                advs.addAll(Arrays.asList(dataArr));

                initViewPager();

                advLayout.setVisibility(View.VISIBLE);
            }
        }
    };

    /**
     * 设置广告图片
     */
    private void initViewPager() {
        listViews = new ArrayList<View>();

        for (int i = 0; i < advs.size(); i++) {
            final AdvInfo advInfo = advs.get(i);

            ImageView imageView = new ImageView(this);
            imageView.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    AdvertisementUtils.open(ClazzLogActivity.this, advInfo);
                }
            });

            String imageUrl = String.format(Config.IMAGE_LOAD_URL_PARAMS, advInfo.getImgPath());
            ImageUtils.loadImage(this, imageView, imageUrl, R.mipmap.empty);

            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            listViews.add(imageView);
        }

        xViewPaper.start(this, listViews, 4000, ovalLayout,
                R.layout.ad_bottom_item, R.id.ad_item_v,
                R.drawable.dot_focused, R.drawable.dot_normal);
    }

    class SectionsPagerAdapter extends FragmentPagerAdapter {
        private String studentId;

        public SectionsPagerAdapter(FragmentManager fm, String studentId) {
            super(fm);

            this.studentId = studentId;
        }

        @Override
        public Fragment getItem(int position) {
            return ClazzLogFragment.newInstance(this.studentId, TYPES[position]);
        }

        @Override
        public int getCount() {
            return NUM_ITEMS;
        }
    }

}
